package com.fr.data.impl;

import com.fr.base.FRContext;
import com.fr.data.TableDataException;
import com.fr.data.core.DataUtils;
import com.fr.data.core.db.ColumnInformation;
import com.fr.data.core.db.dialect.Dialect;
import com.fr.data.core.db.dialect.DialectFactory;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public final class UnCacheAdapter extends TableDataAdapter
{
  private transient int currentRowIndex = -1;
  private transient int _rowCount = 0;
  private transient Object[] currentRowValue = null;
  private transient Object[] lastRowValue = null;

  public UnCacheAdapter(DatabaseConnection paramDatabaseConnection, String paramString)
  {
    super(paramDatabaseConnection, paramString, 0);
  }

  protected void initRowValueStatus()
  {
    this.currentRowIndex = -1;
    this.currentRowValue = new Object[0];
    this._rowCount = 0;
    this.lastRowValue = null;
  }

  protected boolean hasRow(int paramInt)
    throws TableDataException
  {
    if (this.currentRowIndex >= paramInt)
      return true;
    initConnectionAndResultAndCheckInColumns();
    if (this.connection == null)
      return false;
    Dialect localDialect = DialectFactory.generateDialect(this.connection);
    int i = getColumnCount();
    try
    {
      String str1 = this.database.getOriginalCharsetName();
      String str2 = this.database.getNewCharsetName();
      do
      {
        if (!(this.resultSet.next()))
          break label167;
        this.currentRowIndex += 1;
      }
      while (this.currentRowIndex < paramInt);
      this.lastRowValue = this.currentRowValue;
      this.currentRowValue = new Object[i];
      for (int j = 0; j < i; ++j)
        try
        {
          this.currentRowValue[j] = DataUtils.getResultSetObject(this.resultSet, this.columnInformations[j].getColumnType(), j + 1, str1, str2, localDialect);
        }
        catch (SQLException localSQLException)
        {
          this.currentRowValue[j] = null;
        }
      return true;
      label167: this._rowCount = (this.currentRowIndex + 1);
      this.currentRowIndex = -1;
      this.isCheckRowOver = true;
      releaseConnection();
    }
    catch (Exception localException1)
    {
      try
      {
        releaseConnection();
      }
      catch (Exception localException2)
      {
      }
      this.isCheckRowOver = true;
      throw new TableDataException(localException1.getMessage() + "\nQuery is:" + this.sql + "\nrowIndex to get is:" + paramInt, localException1);
    }
    return false;
  }

  public Object getValueAt(int paramInt1, int paramInt2)
    throws TableDataException
  {
    if (this.currentRowIndex < paramInt1)
    {
      hasRow(paramInt1);
    }
    else if (this.currentRowIndex - 1 > paramInt1)
    {
      try
      {
        releaseConnection();
      }
      catch (Exception localException)
      {
        throw new TableDataException(localException.getMessage(), localException);
      }
      this.currentRowIndex = -1;
      hasRow(paramInt1);
    }
    if ((this.currentRowIndex - 1 == paramInt1) && (this.lastRowValue != null) && (this.currentRowValue.length > paramInt2) && (paramInt2 >= 0))
      return this.lastRowValue[paramInt2];
    if ((this.currentRowIndex == paramInt1) && (this.currentRowValue.length > paramInt2) && (paramInt2 >= 0))
      return this.currentRowValue[paramInt2];
    return null;
  }

  public int getRowCount()
    throws TableDataException
  {
    int i = shortcutGetRowCount();
    if (i < 0)
    {
      FRContext.getLogger().log(Level.WARNING, "Can not get the row count!");
      return 0;
    }
    return i;
  }
}